package club.easin.utils;

import club.easin.MYException;
import club.easin.enums.Exenum;
import club.easin.spring.SpringContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.List;

@Repository("DBsubmit")
public class DBSubmit {
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean exce(Tlist tlist){
        if(tlist==null||tlist.getDtatsize()<1) return false;
        List<Tdata> list=tlist.getTdata();
        try {
            for (Tdata tdata : list) {
                String metohName = tdata.getmName();
                String daoName = tdata.getDaoName();
                Class<?> daoClass = SpringContext.getBeanByName(daoName).getClass();
                Object[] value = tdata.getValue();
                for (Method m : daoClass.getMethods()) {
                    if (m.getName().equalsIgnoreCase(metohName)) {
                        System.out.println(value);
                        m.invoke(SpringContext.getBeanByName(daoName), value);
                        break;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new MYException(Exenum.TRANSACTION_ERRPR);
        }

        return true;

    }
}
